package com.hmyuan.webdesktop.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hmyuan.webdesktop.domain.User;
import com.hmyuan.webdesktop.service.UserService;

@RestController
public class IndexController {
	
	@Resource
	UserService userService;

	@RequestMapping("/loginSubmit")
	public Map<String, Object> login(HttpServletRequest rq,HttpServletResponse rp,
			String username,String password){
		Map<String, Object> result = new HashMap<String, Object>();
		if(username==null||password==null) {
			result.put("code", 3);
			result.put("msg", "输入数据不能为空！");
		}else {
			User user = userService.findUserByUsername(username);
			if(user!=null) {
				if(password.equals(user.getPassword())) {
					rq.getSession().setAttribute("current_user", user);
					result.put("code", 0);
					result.put("msg", "登陆成功！");
				}else {
					result.put("code", 3);
					result.put("msg", "用户名或密码错误！");
				}
			}else {
				result.put("code", 3);
				result.put("msg", "用户名不存在！");
			}
		}
		return result;
	}
}
